<template>
  <div>
  <preHeader></preHeader>
  <div class="top-block"></div>
  <mt-field label="分组ID" placeholder="请输入分组ID" v-model='gid' ></mt-field>
  <mt-field label="分组名称" placeholder="请输入分组名称" v-model='name' ></mt-field>
  <mt-button @click='btnSave()'>保存</mt-button>
  </div>
</template>

<script>
  import {proxy_gid_add,proxy_gid_edit,proxy_gid_show_list} from '@/api/';
  export default {
    data(){
      return{
        tel:'',
        type:'',//1修改2添加
        gid:'',
        name:'',
        id:'',
        info:'',//默认获取改条信息
      }
    },
    methods:{
      btnSave(){
        if(this.gid == ''){
          this.$messagebox.alert('分组ID不能为空！')
        }else if(this.name == ''){
          this.$messagebox.alert('分组名称不能为空！')
        }else{
          if(this.type == 1){
            proxy_gid_edit({tel:this.tel,gid:this.gid,gid_name:this.name,id:this.id}).then( res =>{
              // console.log(res)
              if(res.status == 1){
                this.$messagebox.alert('修改成功！').then(action => {
                  if(action == 'confirm'){
                    this.$router.push('/myGroup');
                  }
                });
              }

            })
          }else if(this.type == 2){
            proxy_gid_add({tel:this.tel,gid:this.gid,gid_name:this.name}).then(res =>{
              // console.log(res)
              if(res.status == 1){
                this.$messagebox.alert('添加成功！').then(action => {
                  if(action == 'confirm'){
                    this.$router.push('/myGroup');
                  }
                });
              }
            })
          }
        }

      },
      getInfo(){//获取分组信息
        this.tel = JSON.parse(localStorage.getItem('tel'));
        proxy_gid_show_list({tel:this.tel,id:this.id}).then(res =>{
          // console.log(res)
          this.gid = res.data.gid;
          this.name = res.data.gid_name;
        })
      }

    },
    mounted() {
      this.type = this.$route.query.type;
      this.id = this.$route.query.id;
      this.getInfo();
    }
  }
</script>

<style scoped>
.mint-button{position: fixed;bottom: 1em;left: 50%;transform: translateX(-50%);background-color: #2f3953;width: 150px;border-radius: 20px;color: #fff;}
</style>
